package ms.platform.progress.controller;

import ms.platform.plan.dto.FactoryDto;
import ms.platform.plan.dto.TargetDto;
import ms.platform.plan.model.ModelPlan;
import ms.platform.plan.service.BusModelService;
import ms.platform.progress.dto.*;
import ms.platform.progress.service.FactoryPlanService;
import ms.platform.progress.service.ProgressViewService;
import ms.platform.system.controller.BaseController;
import ms.platform.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 进度查看相关
 * @author Kevin Deng
 *
 */
@Controller
@RequestMapping("/bus/prgview")
public class ProgressViewController extends BaseController{
	
	@Autowired
	ProgressViewService progressViewService;
	@Autowired
	BusModelService busModelService;
	@Autowired
	FactoryPlanService factoryPlanService;
	

	@RequestMapping(value = "/query")
	public String query(@RequestParam Map<String,Object> map, HttpServletRequest request){
		request.setAttribute("map", map);
		List<FactoryDto> factory = busModelService.queryAllFactory("F");
		request.setAttribute("factorys", factory);
		List<ProgressViewVo> list = new ArrayList<>();
		if(map.size() > 0){
			list = progressViewService.viewProgress(map);
		}
		request.setAttribute("list", list);
		List<TargetDto> mTypeList = busModelService.queryAllmodelType();
		request.setAttribute("mTypeList",mTypeList);
		if(list.size() > 0 && list.get(0).getItems().size() > 0){
			request.setAttribute("cloumns", list.get(0).getItems());
			request.setAttribute("size", list.get(0).getItems().size());
		}
		return "progress/progressView";
	}
	/**
	 * 查询台账明细
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryProgessAccount")
    public Object queryProgessAccount(@RequestParam Map<String,Object> map,HttpServletRequest request){
		List<ProgressAccountDto> list =  new ArrayList<>();
		if(map.size()>0){
			list = progressViewService.queryProgessAccount(map);
		}
        return returnSuccess("操作成功",list);
    }
	
	/**
	 * 跳转台账审核明细
	 * @param request
	 * @return
	 */
	@RequestMapping("/toView")
	@ResponseBody
    public Object toView(@RequestParam Map<String,Object> map,HttpServletRequest request){
		ProgressDetailDto detailDto = progressViewService.getProgressDetailById(map.get("recordId").toString(),map.get("recordDetailId").toString());
		Map<String,String> maps = new HashMap<String,String>();
		if(null != detailDto.getModelPlanId()&& StringUtils.isNotEmpty(detailDto.getPlanProgress())){
			maps.put("modelPlanId", detailDto.getModelPlanId().toString());
			maps.put("currentPro", detailDto.getPlanProgress());
			ProgressDto dto = factoryPlanService.getPlanStartTimeByProcessNameTwo(maps);
			long day = DateUtil.getDayByDateSubraction(DateUtil.convertStringToDate(detailDto.getReportDate(), "yyyy-MM-dd"),dto.getPlanStartTime());
			//检查是否 是 计划结束 后
			ModelPlan modelPlan = factoryPlanService.getmodelPlan(detailDto.getModelPlanId());//获取结束时间
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
			String endDate = sdf.format(modelPlan.getPlanEndTime());
			String newDate = sdf.format(new Date());
			//判断当前时间是否大于计划结束时间
			//如果大于，计划比例为100
			if (DateUtil.middate(newDate, endDate, sdf) >= 0) {
				detailDto.setPlanPro("100");
			} else {
				DecimalFormat df = new DecimalFormat("#");
				String planPro = df.format(100/(double)dto.getPlanTotalTime()*day);
				if(Integer.parseInt(planPro)>100){
					detailDto.setPlanPro("100");
				}else{
					detailDto.setPlanPro(df.format(100/(double)dto.getPlanTotalTime()*day));
				}
			}
			if (!StringUtils.isNotBlank(dto.getTargetPre())) {
				detailDto.setTargetPre("0");
			}else{
				detailDto.setTargetPre(dto.getTargetPre());
			}
		}
		if(null != detailDto){
			List<AnnexDto> annexList = factoryPlanService.getProgressAnnexList(detailDto.getId());
			request.setAttribute("annex", annexList);
			detailDto.setAnnexDto(annexList);
		}
        return returnSuccess("操作成功",detailDto);
    }
}
